﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace MLY.LTR
{
    public partial class AlertSet : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["SetNum"] != null)
            {
                if (!IsPostBack)
                {
                    AlertSetDataSource.SelectParameters.Add("AlertSet", Request.QueryString["SetNum"]);
                    AlertSetDataSource.SelectParameters.Add("UserId", SessParam.UserId.ToString());
                }
            }
            else
                Response.Redirect("~/LTR/default.aspx");

            //if (SessParam.IsClient)
            //{
            //    AlertSetGrid.Columns[7].Visible = false;
            //}
            //else
            //{
            //    AlertSetGrid.Columns[8].Visible = false;
            //}
        }

        protected bool IsClient()
        {
            return SessParam.IsClient;
        }

        protected void UpdateAll(object Sender, EventArgs e)
        {
            bool success = true;
            foreach (GridViewRow r in AlertSetGrid.Rows)
            {
                string WorkOrderId = (r.FindControl("WorkOrderIdHiddenField") as HiddenField).Value;
                string Quantity = (r.FindControl("QuantityTextBox") as TextBox).Text.Trim();
                string RelevantDate = SessParam.IsClient ? (r.FindControl("RequestedDelivaryDateTextBox") as TextBox).Text : 
                                                           (r.FindControl("PlannedDelivaryDateTextBox") as TextBox).Text;
                if (RelevantDate.Length > 3)
                {
                    RelevantDate = MLY.Utils.SqlDate(DateTime.Parse(RelevantDate));
                }

                string RelevantWONum = SessParam.IsClient ? (r.FindControl("CustomerWorkOrderNumberTextBox") as TextBox).Text.Trim() :
                                                            (r.FindControl("SupplierWorkOrderNumberTextBox") as TextBox).Text.Trim();
                
                using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MLYConnectionString"].ConnectionString))
                {
                    SqlCommand cmd = new SqlCommand("UpdateWorkOrder", conn);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    cmd.Parameters.Add("WorkOrderId", System.Data.SqlDbType.Int);
                    cmd.Parameters["WorkOrderId"].Value = WorkOrderId;

                    cmd.Parameters.Add("Quantity", System.Data.SqlDbType.Decimal);
                    cmd.Parameters["Quantity"].Value = Quantity;

                    if (RelevantDate.Length > 3)
                    {
                        cmd.Parameters.Add("RelevantDate", System.Data.SqlDbType.DateTime);
                        cmd.Parameters["RelevantDate"].Value = RelevantDate;
                    }

                    cmd.Parameters.Add("RelevantWONumber", System.Data.SqlDbType.NVarChar, 50);
                    cmd.Parameters["RelevantWONumber"].Value = RelevantWONum;

                    cmd.Parameters.Add("IsClient", System.Data.SqlDbType.Bit);
                    cmd.Parameters["IsClient"].Value = SessParam.IsClient;

                    try
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        Log.WriteError(ex.Message);
                        success = false;
                    }
                    finally
                    {
                        conn.Close();
                    }
                }

            }

            AlertSetGrid.DataBind();
            if (success)
                MessageLabel.Text = DateTime.Now.ToShortTimeString() + ": update was successfull";
            else
                MessageLabel.Text = DateTime.Now.ToShortTimeString() + ": there were errors during update";
        }
        
    }
}
